/*
 *  Example program for the Allegro library, by Shawn Hargreaves.
 *
 *  This is a very simple program showing how to get into graphics
 *  mode and draw text onto the screen.
 */

#include "defs.h"
#include "ComponentManager.h"
#include "Component.h"
#include "Window.h"
#include "Button.h"

using namespace gui;

int main(void)
  {
    BITMAP *buffer;
    BITMAP *z_buffer;
    int i, j;

    BITMAP *desktop;

    ComponentManager *manager = ComponentManager::instance();
    Window window;
    Button button;

    if (allegro_init() != 0)
      return 1;

    install_timer();
    install_keyboard();
    install_mouse();

    set_color_depth(32);

    if (set_gfx_mode(GFX_AUTODETECT_WINDOWED, 800, 600, 0, 0) != 0)
      {
        if (set_gfx_mode(GFX_AUTODETECT, 800, 600, 0, 0) != 0)
          {
            if (set_gfx_mode(GFX_SAFE, 320, 200, 0, 0) != 0) 
              {
                set_gfx_mode(GFX_TEXT, 0, 0, 0, 0);
                allegro_message("Unable to set any graphic mode\n%s\n", 
                                allegro_error);
                return 1;
              }
          }
      }

    buffer = create_bitmap(SCREEN_W, SCREEN_H);
    z_buffer = create_bitmap(SCREEN_W, SCREEN_H);
    desktop = load_bitmap(
        "desktop.bmp",
        desktop_palette);

    set_palette(desktop_palette);

    clear_to_color(screen, makecol(0, 0, 0));

    window.init();
    window.move(5, 20);
    window.size(50, SCREEN_H - 25);
    window.moveable(false);
    window.title("Tools");

    button.init();
    button.move(5, 5);
    button.size(40, 40);
    button.title("<T>");
    window.add(button);

    manager->init();
    manager->add(window);

    while (!key[KEY_ESC])
      {
        manager->process();

        clear_bitmap(buffer);

        /* you don't need to do this, but on some platforms (eg. Windows) things
         * will be drawn more quickly if you always acquire the screen before
         * trying to draw onto it.
         */
        acquire_screen();

        stretch_sprite(buffer, desktop, 0, 0, SCREEN_W, SCREEN_H);

        manager->paint(buffer);

        /* painting the cursor */
        triangle(buffer, 
                 mouse_x, mouse_y, 
                 mouse_x+15, mouse_y, 
                 mouse_x, mouse_y+15, 
                 makecol(255, 255, 255));
        line(buffer, mouse_x, mouse_y, mouse_x+15, mouse_y, 0);
        line(buffer, mouse_x, mouse_y, mouse_x, mouse_y+15, 0);
        line(buffer, mouse_x+15, mouse_y, mouse_x, mouse_y+15, 0);

        /* you must always release bitmaps before calling any input functions */
        release_screen();

        blit(buffer, screen, 0, 0, 0, 0, SCREEN_W, SCREEN_H);
        yield_timeslice();
      }

    return 0;
  }

END_OF_MAIN()

